package com.zcp.month2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.zcp.month2.adapter.LeftAdapter;
import com.zcp.month2.adapter.RightAdapter;
import com.zcp.month2.bean.Bean;
import com.zcp.month2.jiekou.CallBack2;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements CallBack2 {

    private RecyclerView left_recycle;
    private RecyclerView right_recycle;
    private ArrayList<String> names;
    private LeftAdapter leftAdapter;
    int position;
    private LinearLayoutManager manager;
    private List<Bean.RsBean> list;
    private LinearLayoutManager manager1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        getData();
        manager = new LinearLayoutManager(MainActivity.this);
        left_recycle.setLayoutManager(manager);
        manager1 = new LinearLayoutManager(MainActivity.this);
        right_recycle.setLayoutManager(manager1);
    }

    private void initView() {
        left_recycle = (RecyclerView) findViewById(R.id.recycle);
        right_recycle = (RecyclerView) findViewById(R.id.recycle2);

    }

    //请求数据
    public void getData()
    {
        names = new ArrayList<String>();
        //创建okHttpClient对象
        OkHttpClient mOkHttpClient = new OkHttpClient();
        //创建一个Request
        final Request request = new Request.Builder()
                .url("http://mock.eoapi.cn/success/4q69ckcRaBdxhdHySqp2Mnxdju5Z8Yr4")
                .build();
        //new call
        Call call = mOkHttpClient.newCall(request);
        //请求加入调度
        call.enqueue(new Callback()
        {
            @Override
            public void onFailure(Request request, IOException e)
            {
            }

            @Override
            public void onResponse(final Response response) throws IOException
            {
                final String htmlStr =  response.body().string();

                runOnUiThread(new Runnable() {
                    private List<Bean.RsBean.ChildrenBeanX> children;

                    @Override
                    public void run() {
                        Gson gson=new Gson();
                        Bean bean = gson.fromJson(htmlStr, Bean.class);
                        list = bean.rs;
                        for (Bean.RsBean rs: list)
                        {
                            String dirName = rs.dirName;
                            names.add(dirName);
                        }

                        //Log.d("zzz",names.toString());


                        leftAdapter = new LeftAdapter(MainActivity.this, names);
                        left_recycle.setAdapter(leftAdapter);

                        leftAdapter.setListener(MainActivity.this);


                        List<Bean.RsBean.ChildrenBeanX> children = list.get(position).children;
                        right_recycle.setAdapter(new RightAdapter(MainActivity.this,children));


                    }
                });
            }
        });
    }


    @Override
    public void getX(int index) {
        Log.d("zzz",index+"*****");

        Bean.RsBean rsBean = list.get(index);
        List<Bean.RsBean.ChildrenBeanX> arr = rsBean.children;
        right_recycle.setAdapter(new RightAdapter(MainActivity.this,arr));
    }
}
